using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace App
{
    public partial class AquaForm : Form
    {

        private Bitmap MyBitmap = null;
        private Form F;

        public AquaForm()
        {
            this.ControlBox = false;
            this.FormBorderStyle = FormBorderStyle.None;
        }

        #region " Layered Window "

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 132)
            {
                m.Result = (IntPtr)(2);
                // Disable this sub for NoMoving Window
                return;
            }
            base.WndProc(ref m);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle = cp.ExStyle | 524288; // This form has to have the WS_EX_LAYERED extended style
                return cp;
            }
        }

        public void SetBitmap(Bitmap bitmap)
        {
            SetBitmap(bitmap, 255);
        }

        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw (new ApplicationException("The bitmap must be 32ppp with alpha-channel."));
            }
            IntPtr screenDc = GetDC(IntPtr.Zero);
            IntPtr memDc = CreateCompatibleDC(screenDc);
            IntPtr hBitmap = IntPtr.Zero;
            IntPtr oldBitmap = IntPtr.Zero;
            try
            {
                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                // grab a GDI handle from this GDI+ bitmap
                oldBitmap = SelectObject(memDc, hBitmap);
                Win32Size size = new Win32Size(bitmap.Width, bitmap.Height);
                Win32Point pointSource = new Win32Point(0, 0);
                Win32Point topPos = new Win32Point(Left, Top);
                BLENDFUNCTION blend = new BLENDFUNCTION();
                blend.BlendOp = AC_SRC_OVER;
                blend.BlendFlags = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat = AC_SRC_ALPHA;
                UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, ULW_ALPHA);
            }
            finally
            {
                ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    SelectObject(memDc, oldBitmap);
                    //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
                    DeleteObject(hBitmap);
                }
                DeleteDC(memDc);
            }
        }

        #endregion

        #region " Procedures "

        public void Prepare(System.Drawing.Point m_Location, Form m_ChildForm)
        {
            m_ChildForm.FormBorderStyle = FormBorderStyle.None;
            m_ChildForm.ControlBox = false;
            F = m_ChildForm;
            //
            this.Location = m_Location;
            this.Icon = m_ChildForm.Icon;
            this.Text = m_ChildForm.Text;
            this.Size = new System.Drawing.Size(F.Width + 30, F.Height + 51);
            F.Location = new System.Drawing.Point(this.Location.X + 15, this.Location.Y + 32);
            //F.Opacity = 0.9;
            F.ShowInTaskbar = false;
            //
            this.TopMost = F.TopMost;
            //
            F.FormClosed += new System.Windows.Forms.FormClosedEventHandler(Handle_ChildFormClose);
            //
            // L15, R15, T32, B19
            MyBitmap = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb);
            Graphics Gr = Graphics.FromImage(MyBitmap);
            //
            Rectangle DestLU = new Rectangle(0, 0, 15, 32);
            Rectangle DestLM = new Rectangle(0, 32, 15, this.Height - 51);
            Rectangle DestLD = new Rectangle(0, this.Height - 19, 15, 19);
            Rectangle DestMU = new Rectangle(15, 0, this.Width - 30, 32);
            Rectangle DestMM = new Rectangle(15, 32, this.Width - 30, this.Height - 51);
            Rectangle DestMD = new Rectangle(15, this.Height - 19, this.Width - 30, 19);
            Rectangle DestRU = new Rectangle(this.Width - 15, 0, 15, 32);
            Rectangle DestRM = new Rectangle(this.Width - 15, 32, 15, this.Height - 51);
            Rectangle DestRD = new Rectangle(this.Width - 15, this.Height - 19, 15, 19);
            //            
            Rectangle SourceLU = new Rectangle(0, 0, 15, 32);
            Rectangle SourceLM = new Rectangle(0, 32, 15, Properties.Resources.Window.Height - 51);
            Rectangle SourceLD = new Rectangle(0, Properties.Resources.Window.Height - 19, 15, 19);
            Rectangle SourceMU = new Rectangle(15, 0, Properties.Resources.Window.Width - 30, 32);
            Rectangle SourceMM = new Rectangle(15, 32, Properties.Resources.Window.Width - 30, Properties.Resources.Window.Height - 51);
            Rectangle SourceMD = new Rectangle(15, Properties.Resources.Window.Height - 19, Properties.Resources.Window.Width - 30, 19);
            Rectangle SourceRU = new Rectangle(Properties.Resources.Window.Width - 15, 0, 15, 32);
            Rectangle SourceRM = new Rectangle(Properties.Resources.Window.Width - 15, 32, 15, Properties.Resources.Window.Height - 51);
            Rectangle SourceRD = new Rectangle(Properties.Resources.Window.Width - 15, Properties.Resources.Window.Height - 19, 15, 19);
            //
            Gr.DrawImage(Properties.Resources.Window, DestLU, SourceLU, GraphicsUnit.Pixel);
            Gr.DrawImage(Properties.Resources.Window, DestLM, SourceLM, GraphicsUnit.Pixel);
            Gr.DrawImage(Properties.Resources.Window, DestLD, SourceLD, GraphicsUnit.Pixel);
            Gr.DrawImage(Properties.Resources.Window, DestMU, SourceMU, GraphicsUnit.Pixel);
            Gr.DrawImage(Properties.Resources.Window, DestMM, SourceMM, GraphicsUnit.Pixel);
            Gr.DrawImage(Properties.Resources.Window, DestMD, SourceMD, GraphicsUnit.Pixel);
            Gr.DrawImage(Properties.Resources.Window, DestRU, SourceRU, GraphicsUnit.Pixel);
            Gr.DrawImage(Properties.Resources.Window, DestRM, SourceRM, GraphicsUnit.Pixel);
            Gr.DrawImage(Properties.Resources.Window, DestRD, SourceRD, GraphicsUnit.Pixel);
            //
            Rectangle TextRect = new Rectangle(15, 8, this.Width - 130, 23);
            Gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            StringFormat SF = new StringFormat();
            SF.Alignment = StringAlignment.Near;
            SF.LineAlignment = StringAlignment.Center;
            Gr.DrawString(m_ChildForm.Text, new Font("Tahoma", 8, FontStyle.Bold), new SolidBrush(Color.FromArgb(50, 50, 50)), TextRect, SF);
        }

        private void Handle_ChildFormClose(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            this.Dispose();
        }

        #endregion

        #region " Form Events "

        //protected override void OnActivated(EventArgs e)
        //{
        //    base.OnActivated(e);
        //    Me_Active = true;
        //    SetBitmap(MyBitmap);
        //}

        //protected override void OnDeactivate(EventArgs e)
        //{
        //    base.OnDeactivate(e);
        //    Me_Active = false;
        //    if (F_Active == false)
        //    {
        //        SetBitmap(MyBitmap, 150);
        //    }
        //}

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            SetBitmap(MyBitmap);
            F.Show(this);
        }

        private void Form_Container_Load(object sender, System.EventArgs e)
        {
            SetBitmap(MyBitmap);
            F.Show(this);
        }

        protected override void OnLocationChanged(EventArgs e)
        {
            base.OnLocationChanged(e);
            F.Location = new System.Drawing.Point(this.Location.X + 15, this.Location.Y + 32);
        }

        #endregion

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // AquaForm
            // 
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Name = "AquaForm";
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Handle_ChildFormClose);
            this.ResumeLayout(false);

        }

        #region Win32 Dll Import
        public const int ULW_COLORKEY = 1;
        public const int ULW_ALPHA = 2;
        public const int ULW_OPAQUE = 4;
        public const byte AC_SRC_OVER = 0;
        public const byte AC_SRC_ALPHA = 1;

        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("gdi32.dll", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern Bool DeleteDC(IntPtr hdc);

        [DllImport("gdi32.dll", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

        [DllImport("gdi32.dll", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

        [DllImport("gdi32.dll", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern Bool DeleteObject(IntPtr hObject);

        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Win32Point pptDst, ref Win32Size psize, IntPtr hdcSrc, ref Win32Point pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);

        [StructLayout(LayoutKind.Sequential)]
        public struct Win32Point
        {
            public int x;
            public int y;

            public Win32Point(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct Win32Size
        {
            public int cx;
            public int cy;

            public Win32Size(int cx, int cy)
            {
                this.cx = cx;
                this.cy = cy;
            }
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct ARGB
        {
            public byte Blue;
            public byte Green;
            public byte Red;
            public byte Alpha;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct BLENDFUNCTION
        {
            public byte BlendOp;
            public byte BlendFlags;
            public byte SourceConstantAlpha;
            public byte AlphaFormat;
        }

        public enum Bool
        {
            F = 0, // False
            T = 1 // True
        }
        #endregion
    }

}